import axios from 'axios';
import {message} from "antd";
import {getToken} from "../utils/user-token";


const instance = axios.create({
    timeout: 1000,
});

instance.interceptors.request.use(config => {
    // Do something before request is sent
    config.headers.Authorization = `Bearer ${getToken()}`;
    return config;
}, error => {
    // Do something with request error
    return Promise.reject(error);
});

instance.interceptors.response.use(res => {
        const resData = (res.data || {}) as ResType
        const {errno, msg, data} = resData;

        if (errno !== 0) {
            //错误提示
            if (msg) {
                message.error(msg)
            }
            throw new Error(msg)
        }

        return data as any;
    }
)

export default instance;

export type ResType = {
    errno: Number,
    data?: ResDataType,
    msg?: string
}
export type ResDataType = {
    [key: string]: any
}